//
//  WCNewStepSheetController.m
//  WabbitCode
//
//  Created by William Towe on 4/16/09.
//  Copyright 2009 Revolution Software. All rights reserved.
//

#import "WCNewStepSheetController.h"
#import "WCBuildTarget.h"
#import "WCBuildTargetGetInfoWindowController.h"
#import "WCProject.h"
#import "NSArray+WCExtensions.h"


@implementation WCNewStepSheetController
- (id)init {
	if (!(self = [super initWithWindowNibName:@"WCNewStepSheet"]))
		return nil;
	
	return self;
}

- (void)dealloc {
	[_filePath release];
	[_name release];
	[super dealloc];
}

- (void)windowDidLoad {
	[super windowDidLoad];
	
	if (_originalBuildStep) {
		[self setName:[[self originalBuildStep] name]];
		[self setFilePath:[[[self originalBuildStep] file] absoluteFilePath]];
		[self setType:[[self originalBuildStep] stepType]];
	}
	else {
		[self setName:NSLocalizedString(@"New Step", @"New Step")];
		[self setFilePath:[[[[[[self getInfoWindowController] project] textFiles] allObjects] firstObject] absoluteFilePath]];
	}
}

+ (void)presentAddStepSheetForBuildTarget:(WCBuildTarget *)target forGetInfoWindowController:(WCBuildTargetGetInfoWindowController *)getInfoWindowController; {
	WCNewStepSheetController *controller = [[[self class] alloc] init];
	
	[controller setBuildTarget:target];
	[controller setGetInfoWindowController:getInfoWindowController];
	
	[NSApp beginSheet:[controller window] modalForWindow:[getInfoWindowController window] modalDelegate:controller didEndSelector:@selector(_sheetDidEnd:code:info:) contextInfo:NULL];
}

+ (void)presentEditStepSheetForBuildTarget:(WCBuildTarget *)target originalBuildStep:(WCBuildStep *)step forGetInfoWindowController:(WCBuildTargetGetInfoWindowController *)getInfoWindowController; {
	WCNewStepSheetController *controller = [[[self class] alloc] init];
	
	[controller setBuildTarget:target];
	[controller setOriginalBuildStep:step];
	[controller setGetInfoWindowController:getInfoWindowController];
	
	[NSApp beginSheet:[controller window] modalForWindow:[getInfoWindowController window] modalDelegate:controller didEndSelector:@selector(_sheetDidEnd:code:info:) contextInfo:NULL];
}

@synthesize buildTarget=_buildTarget,getInfoWindowController=_getInfoWindowController,originalBuildStep=_originalBuildStep,filePath=_filePath,name=_name;

@dynamic type;
- (WCBSStepType)type {
	return [_typePopUpButton selectedTag];
}
- (void)setType:(WCBSStepType)value {
	if ([self type] == value)
		return;
	
	[_typePopUpButton selectItemWithTag:value];
}

- (BOOL)panel:(id)sender shouldShowFilename:(NSString *)filename {
	switch (_type) {
		case WCBSStepTypeRunExecutableFile:
			return ([[NSFileManager defaultManager] isExecutableFileAtPath:filename]);
		case WCBSStepTypeAssembleFile:
		case WCBSStepTypeCreateLabelFile:
		default:			
			return (![[NSWorkspace sharedWorkspace] isFilePackageAtPath:filename]);
	}
}

- (IBAction)chooseFilePath:(id)sender; {
	NSOpenPanel *panel = [NSOpenPanel openPanel];
	
	[panel setPrompt:NSLocalizedString(@"Choose",@"Choose")];
	[panel setDelegate:self];
	
	if ([panel runModalForDirectory:nil file:nil] != NSOKButton)
		return;
	
	[self setFilePath:[[panel filenames] objectAtIndex:0]];
}

- (void)_sheetDidEnd:(NSWindow *)sheet code:(NSInteger)code info:(void *)info {
	[self autorelease];
	if (code != NSOKButton)
		return;
	
	if (_originalBuildStep) {
		WCBuildStep *step = [WCBuildStep buildStepWithStepType:[self type] file:[[[self getInfoWindowController] project] fileMatchingAbsoluteFilePath:[self filePath]]];
		[step setName:[self name]];
		
		[[[self buildTarget] mutableChildNodes] replaceObject:[self originalBuildStep] withObject:step];
	}
	else {
		NSUInteger index = 0;
		if ([[[self getInfoWindowController] buildStepsTableView] selectedRow] != -1)
			index = [[[self getInfoWindowController] buildStepsTableView] selectedRow]+1;
		
		WCBuildStep *step = [WCBuildStep buildStepWithStepType:[self type] file:[[[self getInfoWindowController] project] fileMatchingAbsoluteFilePath:[self filePath]]];
		[step setName:[self name]];
		
		[[[self buildTarget] mutableChildNodes] insertObject:step atIndex:index];
	}
}
@end
